home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / irit / irit.el < prev    next >
Encoding:
Text File  |  1995-01-01  |  11.7 KB  |  301 lines

  1. ; irit.el - Definitions of IRIT mode for emacs editor.
  2. ; Author:    Gershon Elber
  3. ;         Computer Science Dept.
  4. ;         University of Utah
  5. ; Date:    Tue May 14 1991
  6. ; Copyright (c) 1991, Gershon Elber
  7. ;
  8. ; This file defines an environment to run edit and execute IRIT programs.
  9. ; Such a program should have a '.irt' extension in order it to be in
  10. ; irit-mode major mode. Several new functions are provided to communicate
  11. ; between the editted file and the solid modeller:
  12. ;
  13. ; 1. send-line-to-irit - sends a single line to the solid modeller for
  14. ;    execution. A line is defined from current position to the next
  15. ;    semicolon ';'. If however several commands exists on the same line
  16. ;    they will all be send as one line.
  17. ;    Bounded to Meta-E by default.
  18. ; 2. send-region-to-irit - sends the region from the current mark (mark-marker)
  19. ;    to current position (point-marker) to the solid modeller. This function
  20. ;    is convenient for sending a large block of commands.
  21. ;    Bounded to Meta-R by default.
  22. ; 3. send-mini-buffer-to-irit - sends a line retrieved via the mini buffer to
  23. ;    the solid modeller for execution. The line is appended with a new line
  24. ;    and is echoed to the irit-solid-modeller buffer if irit-echo-program.
  25. ;    Bounded to Meta-S by default.
  26. ;
  27. ; Both functions checks for existance of a buffer named irit-solid-modeller
  28. ; and a process named "irit" hooked to it, and will restart a new process
  29. ; or buffer if none exists. The program to execute as process "irit" is
  30. ; defined by the irit-program constant below.
  31. ;
  32.  
  33. (defvar irit-program "irit"
  34.   "*The executable to run for irit-solid-modeller buffer.")
  35.  
  36. (defvar irit-echo-program nil
  37.   "*Control echo of executed commands to irit-solid-modeller buffer.")
  38.  
  39. (defvar irit-mode-map nil "")
  40. (if irit-mode-map
  41.     ()
  42.   (setq irit-mode-map (make-sparse-keymap))
  43.   (define-key irit-mode-map "\M-s" 'send-mini-buffer-to-irit)
  44.   (define-key irit-mode-map "\M-e" 'send-line-to-irit)
  45.   (define-key irit-mode-map "\M-r" 'send-region-to-irit))
  46.  
  47. (define-key c-mode-map "\M-o" 'make-irit-c-function)
  48. (define-key c-mode-map "\M-p" 'overwrite-mode)
  49.  
  50. ;;;
  51. ;;; Define the irit-mode
  52. ;;;
  53. (defun irit-mode ()
  54.   "Major mode for editing and executing IRIT files.
  55.  
  56. see send-line-to-irit and send-region-to-irit for more."
  57.   (interactive)
  58.   (use-local-map irit-mode-map)
  59.   (setq major-mode 'irit-mode)
  60.   (setq mode-name "Irit")
  61.   (run-hooks 'irit-mode-hook))
  62.  
  63. ;;;
  64. ;;; Define send-min-buffer-to-irit - send one line prompt for at the mini
  65. ;;; buffer, to the irit buffer.
  66. ;;;
  67. (defun send-mini-buffer-to-irit ()
  68.   "Sends one line of code from mini-buffer to the IRIT program.
  69.  
  70. The IRIT solid modeller buffer name is irit-solid-modeller and the 
  71. process name is 'irit'. If none exists, a new one is created.
  72.  
  73. The name of the irit program program to execute is stored in irit-program
  74. and may be changed."
  75.   (interactive)
  76.   (if (equal major-mode 'irit-mode)
  77.     (progn
  78.       (make-irit-buffer)     ; In case we should start a new one.
  79.       (let* ((crnt-buffer (buffer-name))
  80.          (string-copy (read-from-minibuffer "Irit> ")))
  81.     (switch-to-buffer-other-window (get-buffer "irit-solid-modeller"))
  82.     (end-of-buffer)
  83.     (if irit-echo-program
  84.       (insert string-copy))
  85.     (if (not (pos-visible-in-window-p))
  86.       (recenter 3))
  87.     (process-send-string "irit" string-copy)
  88.     (process-send-string "irit" "\n")
  89.     (switch-to-buffer-other-window (get-buffer crnt-buffer))))
  90.     (message "Should be invoked in irit-mode only.")))
  91. ;;;
  92. ;;; Define send-line-to-irit - send from current cursor position to next
  93. ;;; semicolon detected.
  94. ;;;
  95. (defun send-line-to-irit ()
  96.   "Sends one line of code from current buffer to the IRIT program.
  97.  
  98. Use to execute a line in the IRIT solid modeller. A line is anything
  99. that is terminated by a semicolon, but is at least one line of text so
  100. multiple commands per line (with several semicolons) are still
  101. considered a single line.
  102.  
  103. The IRIT solid modeller buffer name is irit-solid-modeller and the 
  104. process name is 'irit'. If none exists, a new one is created.
  105.  
  106. The name of the irit program program to execute is stored in irit-program
  107. and may be changed."
  108.   (interactive)
  109.   (if (equal major-mode 'irit-mode)
  110.     (progn
  111.       (make-irit-buffer)        ; In case we should start a new one.
  112.       (beginning-of-line)
  113.       (let ((start-mark (point-marker)))
  114.     (search-forward ";")
  115.     (let ((end-one-mark (point-marker)))
  116.       (goto-char start-mark)
  117.       (beginning-of-line)
  118.       (next-line 1)
  119.       (let* ((crnt-buffer (buffer-name))
  120.              (end-two-mark (point-marker))
  121.              (end-max-mark (max end-one-mark end-two-mark))
  122.          (string-copy (buffer-substring start-mark end-max-mark)))
  123.         (switch-to-buffer-other-window (get-buffer "irit-solid-modeller"))
  124.         (end-of-buffer)
  125.         (if irit-echo-program
  126.           (insert string-copy))
  127.         (set-marker (process-mark (get-process "irit")) (point-marker))
  128.         (if (not (pos-visible-in-window-p))
  129.           (recenter 3))
  130.         (switch-to-buffer-other-window (get-buffer crnt-buffer))
  131.         (process-send-region "irit" start-mark end-max-mark)
  132.         (goto-char end-max-mark)
  133.         (if (equal "\n" (buffer-substring (point-marker)
  134.                           (+ 1 (point-marker))))
  135.           (process-send-string "irit" "\n"))  
  136.         (if (> end-one-mark end-two-mark)
  137.           (forward-char 1))))))
  138.     (message "Should be invoked in irit-mode only.")))
  139.  
  140. ;;;
  141. ;;; Define send-region-to-irit - send from current cursor position to
  142. ;;; current marker.
  143. ;;;
  144. (defun send-region-to-irit ()
  145.   "Sends a region of code from current buffer to the IRIT program.
  146.  
  147. When this function is invoked on an IRIT file it send the region from current
  148. point to current mark to the irit solid modeller.
  149.  
  150. The IRIT solid modeller buffer name is irit-solid-modeller and the
  151. process name is 'irit'. If none exists, a new one is created.
  152.  
  153. The name of the irit program program to execute is stored in irit-program
  154. and may be changed."
  155.   (interactive)
  156.   (if (equal major-mode 'irit-mode)
  157.     (progn
  158.       (make-irit-buffer)     ; In case we should start a new one.
  159.       (copy-region-as-kill (mark-marker) (point-marker))
  160.       (let ((crnt-buffer (buffer-name)))
  161.     (switch-to-buffer-other-window (get-buffer "irit-solid-modeller"))
  162.     (end-of-buffer)
  163.     (if irit-echo-program
  164.       (yank))
  165.     (set-marker (process-mark (get-process "irit")) (point-marker))
  166.     (if (not (pos-visible-in-window-p))
  167.       (recenter 3))
  168.     (switch-to-buffer-other-window (get-buffer crnt-buffer))
  169.     (process-send-region "irit" (mark-marker) (point-marker))))
  170.     (message "Should be invoked in irit-mode only.")))
  171.  
  172. ;;;
  173. ;;; Switch to "irit-solid-modeller" buffer if exists. If not, creates one and
  174. ;;; execute the program defined by irit-program.
  175. ;;;
  176. (defun make-irit-buffer ()
  177.   "Switch to irit-solid-modeller buffer or create one if none exists"
  178.   (interactive)
  179.   (if (not (get-process "irit"))
  180.     (start-process "irit" "irit-solid-modeller" irit-program)))
  181.  
  182. ;;;
  183. ;;; Autoload irit-mode on any file with irt extension. 
  184. ;;;
  185. (setq auto-mode-alist (append '(("\\.irt$" . irit-mode))
  186.                   auto-mode-alist))
  187.  
  188. ;;;
  189. ;;; Gets a single function's parameter containing both type andname and
  190. ;;; isolate the parametr's name out of it.
  191. ;;;
  192. (defun make-irit-c-isolate-var-name (type-and-name)
  193.   (let* ((match1 (string-match "[^\*     ]+" type-and-name))
  194.      (match2 (string-match "[\[     ]+" type-and-name match1))
  195.      (match3 (string-match "[^\*     ]+" type-and-name match2))
  196.      (match4 (string-match "[\[     ]+" type-and-name match3)))
  197.     (if match3
  198.       (substring type-and-name match3 (if match4
  199.                     match4
  200.                     (length type-and-name)))
  201.       (substring type-and-name match1 (if match2
  202.                     match2
  203.                     (length type-and-name))))))
  204.  
  205. ;;;
  206. ;;; Given a whole describing the arguments, split it into the individual
  207. ;;; parameters by searching for commas and close parenthesis.
  208. ;;;
  209. (defun make-irit-c-func-parse-args (args)
  210.   (if (string-match "([     ]*void[     ]*)" args)
  211.     nil
  212.     (let ((match (string-match "[,)]" args)))
  213.       (if match
  214.     (cons (make-irit-c-isolate-var-name (substring args 1 match))
  215.           (make-irit-c-func-parse-args
  216.                          (substring args (+ match 1) (length args))))))))
  217.  
  218. ;;;
  219. ;;; Insert a list describing the arguments into the current buffer.
  220. ;;;
  221. (defun make-irit-c-func-ins-params (args term-ch)
  222.   (if args
  223.     (progn
  224.       (insert (concat "*   " (car args)))
  225.       (insert-char ?: 1)
  226.       (insert-char ?  (- 72 (length (car args))))
  227.       (insert term-ch)
  228.       (make-irit-c-func-ins-params (cdr args) term-ch))))
  229.  
  230. ;;;
  231. ;;; Filters out trailing spaces if it is not a pointer (no *)
  232. ;;;
  233. (defun make-irit-c-func-retval (retval)
  234.   (let* ((match1 (string-match "*+" retval))
  235.      (match2 (string-match "[     ]+" retval)))
  236.     (if match1
  237.       retval
  238.       (if match2
  239.         (substring retval 0 match2)
  240.     retval))))
  241.  
  242. ;;;
  243. ;;; Make a skeleton header for an IRIT C source function.
  244. ;;;
  245. (defun make-irit-c-function ()
  246.   "Creates a sketelon for a C function for the IRIT solid modeler C code"
  247.   (interactive)
  248.   (if (equal major-mode 'c-mode)
  249.     (progn
  250.       (let* ((func-proto (read-from-minibuffer "Function Prototype: "))
  251.          (match1 (string-match "[     ]+" func-proto))
  252.          (match2 (string-match "[^*     ]+" func-proto match1))
  253.          (match3 (string-match "[     ]+" func-proto match2))
  254.          (match4 (string-match "[^*     ]+" func-proto match3))
  255.          (match5 (string-match "(" func-proto))
  256.          (ret-val (if (and match3 (< match3 match5))
  257.             (make-irit-c-func-retval (substring func-proto match2
  258.                                           match4))
  259.             (make-irit-c-func-retval (substring func-proto 0
  260.                                                                       match2))))
  261.          (term-ch (if (string-match "static" (substring func-proto 0 match1))
  262.             "*\n"
  263.             "M\n"))
  264.          (func-name (if (and match3 (< match3 match5))
  265.               (substring func-proto match4 match5)
  266.               (substring func-proto match2 match5)))
  267.          (args (make-irit-c-func-parse-args
  268.             (substring func-proto match5 (length func-proto)))))
  269.         (insert "/*****************************************************************************\n")
  270.         (insert (concat "* DESCRIPTION:                                                               " term-ch))
  271.         (insert (concat "*                                                                            " term-ch))
  272.         (insert (concat "*                                                                            " term-ch))
  273.         (insert "*                                                                            *\n")
  274.         (insert (concat "* PARAMETERS:                                                                " term-ch))
  275.     (if args
  276.       (make-irit-c-func-ins-params args term-ch)
  277.       (insert (concat "*   None                                                                     " term-ch)))
  278.         (insert "*                                                                            *\n")
  279.         (insert (concat "* RETURN VALUE:                                                              " term-ch))
  280.     (insert (concat "*   " ret-val ))
  281.     (if (string-match "void" ret-val)
  282.       (insert-char ?  (- 73 (length ret-val)))
  283.       (progn
  284.         (insert-char ?: 1)
  285.         (insert-char ?  (- 72 (length ret-val)))))
  286.     (insert term-ch)
  287.         (insert (concat "*                                                                            " term-ch))
  288.         (insert "*                                                                            *\n")
  289.     (if (not (string-match "static" (substring func-proto 0 match1)))
  290.       (progn
  291.         (insert (concat "* KEYWORDS:                                                                  " term-ch))
  292.         (insert (concat "*   " func-name))
  293.         (insert-char ?  (- 73 (length func-name)))
  294.         (insert term-ch)))
  295.         (insert (concat "*                                                                            " term-ch))
  296.         (insert "*****************************************************************************/\n")
  297.     (insert (concat func-proto "\n{\n}\n\n"))))
  298.     (message "Should be invoked in C-mode only.")))
  299.